package org.example.librarysystemspringboot.service.impl;

import org.example.librarysystemspringboot.entity.model.Book;
import org.example.librarysystemspringboot.dao.BookMapper;
import org.example.librarysystemspringboot.repository.BookRepository;
import org.example.librarysystemspringboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import java.util.ArrayList;
import java.util.List;


@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
@Autowired
private BookMapper bookMapper;
    @Override
    public List<Book> getBooksByType(String TypeId, int page, int limit) {
        List<Book> allBooks = bookRepository.findBooksByTypeId(TypeId);
        int startIndex = (page - 1) * limit;
        int endIndex = Math.max(startIndex + limit,allBooks.size());
        return allBooks.subList(startIndex, endIndex);
    }

    @Override
    public int getTotalBooksByType(String TypeId) {
        return bookRepository.findBooksByTypeId(TypeId).size();
    }

    @Override
    public List<Book> getAllBooks(String typeId, int page, int limit) {
        List<Book> allBooks = bookMapper.getAllBooks(typeId,page,limit);
//        int i = bookRepository.findBooksByTypeId(typeId).size()/limit;

        return  allBooks;

    }

    @Override
    public List<Book> selectBooks(Book book) {
      return  bookMapper.selectBooks(book);
    }


}
